// pages/info/index.js
import {
    form
} from '../../api/apiData'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        show: false,
        columns: ['杭州', '宁波', '温州', '嘉兴', '湖州'],
        formList: [],
        cascaderValue: '',
        options: [],
        fieldNames: {
            text: 'label',
            value: 'value',
            children: 'children',
        },
        obj: {},
        inputList: [],
        obj: '',
        id: '',
        selIndex: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getForm(options.id)
        this.setData({
            id: options.id
        })
        if (options.val) {
            this.setData({
                val: options.val
            })
        }
    },
    getForm(id) {
        form.detail({
            form_id: id
        }, res => {
            if (res.code == 1) {
                let datas = JSON.parse(res.data.rules)
                let options = JSON.parse(res.data.options)
                let rule = options.form.page_rule
                datas.forEach(item => {
                    item.value = ''
                    item.value_text = ''
                })
                let newArr = []
                newArr = datas.slice(0, rule)
                // if (id == 1) {
                //     newArr = datas.slice(0, 2)

                // } else {
                //     newArr = datas.slice(0, 3)
                // }
                if (id == 2&&this.data.val) {
                    // console.log(88)
                    let optionsArr = newArr[2].props.options
                    let value_text = ''
                    let value = ''
                    optionsArr.forEach(item => {
                        item.children.forEach(item2 => {
                            if (item2.value == this.data.val) {
                                value_text = item2.label
                                value = [item.value,item2.value]
                            }
                        })
                    })
                    newArr[2].value_text = value_text
                    newArr[2].value = value
                }
                this.setData({
                    formList: newArr
                })
                // console.log(newArr)
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },
    submit() {
        let array = this.data.formList
        const result = {};
        let arrValue = []
        if (!this.validStr(array[0].value, 2)) {
            wx.showToast({
                title: '请输入正确姓名',
                icon: 'none'
            })
            return false
        }

        array.forEach(item => {
            if (!result[item.field]) {
                result[item.field] = [];
            }
            result[item.field] = item.value;
            if (item.value != '') {
                arrValue.push(item.value)
            }
        });
        let obj = JSON.stringify(result)
        if (arrValue.length == array.length) {
            wx.navigateTo({
                url: '/pages/question/index?obj=' + obj + '&id=' + this.data.id,
            })
        } else {
            wx.showToast({
                title: '请完整填写信息',
                icon: 'error'
            })
        }
    },
    validStr(str, length) {
        length = parseInt(length);
        // 使用正则表达式匹配汉字
        const reg = /^[\u4e00-\u9fa5]+$/;
        // 判断字符串是否为汉字且长度大于等于length
        return reg.test(str) && str.length >= length;
    },
    onCancel() {
        this.setData({
            show: false
        })
    },
    onConfirm(e) {
        // console.log(e)
        let index = this.data.selIndex
        const {
            selectedOptions,
            value
        } = e.detail;
        const fieldValue = selectedOptions
            .map((option) => option.label || option.name)
            .join('/');
        let arr = []
        let text = []
        // console.log(selectedOptions)
        selectedOptions.forEach(item => {
            arr.push(item.value)
            text.push(item.label)
        })
        if (this.data.formList[index].type == "select") {
            arr = arr.toString()
        }
        this.setData({
            ['formList[' + index + '].value']: arr,
            ['formList[' + index + '].value_text']: text.join('/')
        })
        this.setData({
            fieldValue,
            cascaderValue: value,
            show: false,
        })
    },
    // 获取文本输入值
    onInputChange(e) {
        let obj1 = this.data.obj
        let value = e.detail.value
        let index = e.currentTarget.dataset.index
        let key = e.currentTarget.dataset.key
        let obj2 = {}
        obj2[key] = value
        let obj = Object.assign({}, obj1, obj2);
        this.setData({
            obj,
            ['formList[' + index + '].value']: value
        })
    },
    toShow(e) {
        if(this.data.val&&this.data.id==2){
            return false
        }
        let index = e.currentTarget.dataset.index
        let options = []
        if (this.data.formList[index].type == "cascader") {
            options = this.data.formList[index].props.options
        } else {
            options = this.data.formList[index].options
        }
        let valArr = []
        if (this.data.val && this.data.id == 1) {
            options.forEach(item => {
                if (item.value == this.data.val) {
                    valArr.push(item)
                }
            })
            options = valArr
            // options=[options[this.data.op_index]]
        }
        if (this.data.val && this.data.id == 2) {
            options.forEach(item => {
                item.children.forEach(item2 => {
                    if (item2.value == this.data.val) {
                        valArr.push(item2)
                    }
                })

            })
            options = valArr
            // options=[options[this.data.op_index]]
        }
        this.setData({
            show: true,
            selIndex: index,
            options
        })
    },
    toUrl() {
        wx.navigateTo({
            url: '/pages/question/index',
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})